import {
  Controller,
  Get,
  Post,
  Query,
  Body,
  Patch,
  Param,
  Delete,
  UseGuards,
} from '@nestjs/common';
import { RoleService } from './role.service';
import {
  ApiResponse,
  ApiOperation,
  ApiTags,
  ApiBearerAuth,
} from '@nestjs/swagger';
import { Search, SearchParams } from 'src/common/decorators/search.decorator';

import { RoleEntity } from './entities/role.entity';
import { CreateRoleDto } from './dto/create-role.dto';
import { UpdateRoleDto } from './dto/update-role.dto';
import { ListRoleDto, ListRoleReturn } from './dto/list-role.dto';
import { JwtAuthGuard } from '../../common/guards/jwt.guard';

@ApiTags('角色模块')
@ApiBearerAuth()
@Controller('role')
@UseGuards(JwtAuthGuard)
export class RoleController {
  constructor(private readonly roleService: RoleService) {}

  @Post()
  @ApiOperation({ summary: '创建角色' })
  @ApiResponse({ type: RoleEntity })
  create(@Body() createRoleDto: CreateRoleDto) {
    return this.roleService.create(createRoleDto);
  }

  @Get('list')
  @ApiOperation({ summary: '查询角色列表' })
  @ApiResponse({ type: ListRoleReturn })
  findAll(
    @Query() searchDto: ListRoleDto,
    @SearchParams() searchParams: Search,
  ): Promise<ListRoleReturn> {
    return this.roleService.findAll(searchParams);
  }

  @Get(':id')
  @ApiOperation({ summary: '查询角色详情' })
  @ApiResponse({ type: RoleEntity })
  findOne(@Param('id') id: string): Promise<RoleEntity> {
    return this.roleService.findOne(id);
  }

  @Patch(':id')
  @ApiOperation({ summary: '更新角色' })
  @ApiResponse({ example: 'success' })
  update(@Param('id') id: string, @Body() updateRoleDto: UpdateRoleDto) {
    return this.roleService.update(id, updateRoleDto);
  }

  @Delete(':id')
  @ApiOperation({ summary: '删除角色' })
  @ApiResponse({ example: 'success' })
  remove(@Param('id') id: string) {
    return this.roleService.remove(id);
  }
}
